package com.culture.bootdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.culture.bootdemo.model.dto.CommonDTO;
import com.culture.bootdemo.po.Answer;
import com.culture.bootdemo.utils.ListSelfUtil;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wen
 * @date 2020/1/2 14:26
 * @Description
 **/
public class Java8Demo {

    public static void test() {
        System.out.println("=====collect:将流转换为其他形式:list");
        List<Integer> list = Stream.iterate(1, x -> x + 1).limit(100).collect(Collectors.toList());
        System.out.println(list);
        System.out.println("=====collect:将流转换为其他形式:set");
        Set<Integer> set = new HashSet<>(Arrays.asList(1, 1, 2, 2, 3, 3, 3));
        System.out.println(set);
        System.out.println("=====collect:将流转换为其他形式:TreeSet");
        TreeSet<Integer> treeSet = new TreeSet<>(Arrays.asList(1, 1, 2, 2, 3, 3, 3));
        TreeSet<Integer> treeSet1 = new TreeSet<>(Arrays.asList(1, 1, 2, 2, 3, 3, 3));
        System.out.println(treeSet);
        System.out.println("=====collect:将流转换为其他形式:map");
        Map<Integer, Integer> map = Stream.iterate(1, x -> x + 1).limit(100).collect(Collectors.toMap(Integer::intValue, Integer::intValue));
        System.out.println(map);
        System.out.println("=====collect:将流转换为其他形式:sum");
        Integer sum = Stream.iterate(1, x -> x + 1).limit(100).mapToInt(Integer::intValue).sum();
        Integer sum2 = Stream.iterate(1, x -> x + 1).limit(100).mapToInt(Integer::intValue).sum();
        System.out.println(sum);
        System.out.println("=====collect:将流转换为其他形式:avg");
        Double avg = Stream.iterate(1, x -> x + 1).limit(100).collect(Collectors.averagingInt(Integer::intValue));
        System.out.println(avg);
        System.out.println("=====collect:将流转换为其他形式:max");
        Optional<Integer> max = Stream.iterate(1, x -> x + 1).limit(100).max(Integer::compareTo);
        Optional<Integer> max2 = Stream.iterate(1, x -> x + 1).limit(100).max(Integer::compareTo);
        System.out.println(max.get());
        System.out.println("=====collect:将流转换为其他形式:min");
        Optional<Integer> min = Stream.iterate(1, x -> x + 1).limit(100).min(Comparator.comparingInt(x -> x));
        Optional<Integer> min2 = Stream.iterate(1, x -> x + 1).limit(100).min((x, y) -> x - y);
        System.out.println(min.get());
    }

    /**
     * list转map
     */
    public static void test3() {
        List<Answer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
//            if (i == 2) {
//                Answer answer = new Answer(i, "data当前是" + i);
//                list.add(answer);
//            }
            Answer answer = new Answer(i, "data" + i);
            list.add(answer);
        }
        Map<Integer, List<Answer>> collect = list.stream().collect(Collectors.groupingBy(Answer::getId));
        Map<Integer, String> collect1 = list.stream().collect(Collectors.toMap(Answer::getId, Answer::getData, (v1, v2) -> v2));
        Map<Integer, Answer> collect2 = list.stream().collect(Collectors.toMap(Answer::getId, Function.identity(), (v1, v2) -> v2));
        Map<Integer, Set<String>> collect3 = list.stream().collect(Collectors.groupingBy(Answer::getId, Collectors.mapping(Answer::getData, Collectors.toSet())));


        System.out.println(collect);
        System.out.println(collect1);
        System.out.println(collect2);
        System.out.println(collect3);
    }

    /**
     * custIds集合 减去在initList集合里的数据
     */
    public static void test2() {
        List<String> initList = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7"));
        List<String> custIds = new ArrayList<>(Arrays.asList("1", "2", "3", "9", "10", "11", "12"));
        List<String> list = ListSelfUtil.removeAllASubtractB(custIds, initList);
        System.out.println(JSON.toJSONString(list));
    }

    /**
     * list-vo转换
     */
    public static void test4() {
        List<CommonDTO> list = new ArrayList<>();
        List<Answer> list2 = list.stream().map(po -> {
            Answer answer = new Answer();
            BeanUtils.copyProperties(po, answer);
            answer.setAnswerId(123);
            return answer;
        }).collect(Collectors.toList());

    }


    public static void main(String[] args) {
        test3();
    }
//    public static void main(String[] args) {
//        Optional<String> o = Optional.ofNullable("hello");
//        String o1 = o.get();
//        //todo  暂未清楚
//        String s = o.flatMap(data -> Optional.of("hello")).orElse("wusong");
//        String s1 = o.flatMap(data -> Optional.of("laiyifa")).orElse("wusong");
//        System.out.println(o1);
//        System.out.println(s);
//        System.out.println(s1);
//    }
}
